/*
 *  $TEAM_NAME Space Trader
 */
package presenters;

import models.Galaxy;
import models.Player;
import models.Game;
import models.StarSystem;
import views.MapPanel;
import views.MapOverlayPanel;
import views.StarSystemPanel;
import java.util.Observer;
import java.util.Observable;
import java.awt.Component;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;

/**
 * Handles the presentation of the galaxy map to the player.
 * @author bradley
 * @version 1.0
 */
public class MapPresenter extends InteractivePresenter<MapPanel> 
implements Observer, MouseListener {
	/**
	 * field
	 */
	private final Galaxy galaxy; 

	/**
	 * field
	 */
	private final Player player;
    
	/**
	 * field
	 */
	private final Game game;
	
	//@SuppressWarnings("unused") // can remove in future
	//private final StarSystem highlightedStarSystem = null;
    /**
     * map presenter
     * @param player
     * @param galaxy
     * @param game
     * @param mapPanel
     * @param frame
     */
    public MapPresenter(Player player, Galaxy galaxy, 
    		Game game, MapPanel mapPanel, JFrame frame) {
    	//Set local variables
    	galaxy.addObserver(this);
        this.galaxy = galaxy;
        player.addObserver(this);
        this.player = player;
        
        this.game = game;

        super.setFrame(frame);
        //Set the map panel that this should present
        mapPanel.addMouseListener(this);
        super.setView(mapPanel);
        this.updateView();
    }

    /**
     * Opens a popup windows to view the systems properties and warp to the system.
     * @param ss - the solar system to view the properties of
     */
    public void highlightStarSystem(StarSystem ss) {
        if (ss == null) {
        	//Remove the overlay
            view.removeOverlay(); 
        } else {
        	//Add the overlay to the view
            final MapOverlayPanel overlay = new MapOverlayPanel(ss);
            view.addOverlay(overlay);
            
            //Present the overlay
            final MapOverlayPresenter mp = new MapOverlayPresenter(
            		this, ss, player, game, overlay);
            mp.setFrame(frame);
        }
    }

    /**
     * Removes the overlay when the mouse exits the popup window
     * @param event - the event of the mouse moving out of the window
     */
    public void mouseEntered(MouseEvent event) {
        view.removeOverlay();
    }

    /**
     * Getter for the galaxy
     * @return - gets the galaxy
     */
    public Galaxy getGalaxy() {
        return galaxy;
    }

    /**
     * Updates the view associated with the map presenter
     */
    public final void updateView() {
    	//update the view
        view.update();

        // Attach controllers to the GalaxyView's subviews
        for (Component component : view.getComponents()) {
            if (component instanceof StarSystemPanel) {
                StarSystemPanel panel = (StarSystemPanel) component;
                StarSystemPresenter sp = new StarSystemPresenter(
                		this, panel.getStarSystem(), panel);
                System.out.print(sp.toString());
            }
        }
    }

    /**
     * update the presenter
     * @param o - observable 
     * @param arg - object
     */
    public void update(Observable o, Object arg) {
        updateView();
    }
    
    /**
     * to string
     * @return string
     */
    public String toString() {
		return "";
	}
}
